package com.woniu.controller;


import com.woniu.entity.Role;
import com.woniu.model.Result;
import com.woniu.service.RoleService;
import com.woniu.service.vo.TestPageVo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author D
 * @since 2023-03-27 08:24:57
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    RoleService roleService;

    /**
     * 查询角色及关联权限
     *
     * @param currentPage 当前页码
     * @param pageSize    每页条数
     * @param keyword     关键字
     * @return
     */
    @RequestMapping("/selectList")
    public Result<TestPageVo> selectList(Integer currentPage, Integer pageSize, String keyword) {
        TestPageVo pageVo = roleService.selectList(currentPage, pageSize, keyword);
        return Result.successResult(pageVo);
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping("/addRole")
    public Result<String> addRole(Role role) {
        role.setInsertTime(new Date()).setUpdateTime(new Date());
        roleService.save(role);
        return Result.successResult("ok");
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping("/deleteRole/{id}")
    public Result<String> deleteRole(@PathVariable("id") Long id){
        roleService.deleteRole(id);
        return Result.successResult("ok");
    }

    /**
     * 修改角色信息
     * @param role
     * @return
     */
    @RequestMapping("/updateRole")
    public Result<String> updateRole(Role role){
        role.setUpdateTime(new Date());
        roleService.updateById(role);
        return Result.successResult("ok");
    }
}

